package cn.wenzy.aurora.framework.configuration;

import cn.hutool.extra.spring.EnableSpringUtil;
import cn.wenzy.aurora.framework.support.cache.CacheSupport;
import cn.wenzy.aurora.framework.support.i18n.properties.I18nProperties;
import cn.wenzy.aurora.framework.support.i18n.core.I18nMessageLoader;
import cn.wenzy.aurora.framework.support.i18n.core.I18nMessageSource;
import cn.wenzy.aurora.framework.support.i18n.I18nSupport;
import cn.wenzy.aurora.framework.support.i18n.serializer.I18nContextValueFilter;
import cn.wenzy.aurora.framework.support.validation.ValidationSupport;
import cn.wenzy.aurora.framework.support.validation.interpolation.MessageInterpolator;
import cn.wenzy.aurora.framework.support.validation.properties.ValidationProperties;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import org.hibernate.validator.HibernateValidator;
import org.redisson.api.RedissonClient;
import org.redisson.spring.starter.RedissonAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator;

@EnableAsync
@Configuration
@EnableSpringUtil()
@AutoConfigureAfter(RedissonAutoConfiguration.class)
@EnableConfigurationProperties({I18nProperties.class, ValidationProperties.class})
public class AutoConfiguration {
    private final I18nProperties i18nProperties;
    private final ValidationProperties validationProperties;

    public AutoConfiguration(I18nProperties i18nProperties, ValidationProperties validationProperties) {
        this.i18nProperties = i18nProperties;
        this.validationProperties = validationProperties;
    }

    @Bean
    public CacheSupport cacheSupport(RedissonClient redissonClient) {
        return new CacheSupport(redissonClient);
    }



    @Bean
    public I18nMessageLoader i18nMessageLoader(){
        I18nMessageLoader messageLoader = new I18nMessageLoader(i18nProperties);
        messageLoader.load();
        return messageLoader;
    }

    @Bean
    public MessageSource messageSource(I18nMessageLoader messageLoader, CacheSupport cacheSupport) {
        return new I18nMessageSource(messageLoader, i18nProperties, cacheSupport);
    }

    @Bean
    public I18nSupport i18nSupport(MessageSource messageSource){
        return new I18nSupport(messageSource);
    }

    @Bean
    public I18nContextValueFilter i18nContextValueFilter(I18nSupport i18nSupport){
        return new I18nContextValueFilter(i18nSupport);
    }

    @Bean
    public Validator validator(MessageSource messageSource) {
        return Validation.byProvider(HibernateValidator.class)
                .configure()
                .failFast(false)
                .messageInterpolator(new MessageInterpolator(new MessageSourceResourceBundleLocator(messageSource)))
                .buildValidatorFactory()
                .getValidator();
    }

    @Bean
    public ValidationSupport validationSupport(I18nSupport i18nSupport, Validator validator) {
        return new ValidationSupport(i18nSupport, validationProperties, validator);
    }



}
